package com.momo.seckilling.service;

import com.momo.seckilling.dao.UserDao;
import com.momo.seckilling.entity.UserInfo;
import com.momo.seckilling.form.UserRegisterForm;
import com.momo.seckilling.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by li on 2019/2/2.
 */
@Service
public class UserService {
    @Autowired
    private UserDao userdao;

    public UserInfo findUserByid(int userid){
        return userdao.findUserByid(userid);
    }

    public void saveUserInfo(UserRegisterForm userRegisterForm) throws Exception {
        UserInfo userInfo = new UserInfo();
        userInfo.setAccount(userRegisterForm.getAccount());
        userInfo.setBirthday(userRegisterForm.getBirthday());
        userInfo.setTelphone(userRegisterForm.getTelphone());
        userInfo.setQq(userRegisterForm.getQq());
        userInfo.setWeixin(userRegisterForm.getWeinxin());
        String password = userRegisterForm.getPassword();
        userInfo.setOriginalPassword(password);
        userInfo.setEncryptionPassword(Md5Util.md5(password,Md5Util.md5key));
        userdao.saveUserInfo(userInfo);
    }

    public boolean verifyUserAccount(UserRegisterForm userRegisterForm) throws Exception {
        String account = userRegisterForm.getAccount();
        String password = userRegisterForm.getPassword();
        UserInfo userInfo = userdao.findUserByAccount(account);
        String encryptionPassword = userInfo.getEncryptionPassword();
        return Md5Util.verify(password,Md5Util.md5key,encryptionPassword);
    }
}
